﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace XamlingCore.Infrastructure.LocalStorage
{
    public class LocalStorageHelper
    {
        public static void ClearStorage()
        {
            recurseDelete("");
        }

        public static void Delete(string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if(isf.FileExists(fileName))
                    isf.DeleteFile(fileName);
            }
        }

        public static List<string> ListFiles(string directory)
        {
            var result = new List<string>();
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.DirectoryExists(directory))
                {
                    foreach (var fileName in isf.GetFileNames(directory + "/*.*"))
                    {
                        result.Add(fileName);
                    }    
                }
                
            }
            return result;
        }


        static void recurseDelete(string directory)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                foreach (var dirName in isf.GetDirectoryNames(directory + "/*"))
                {
                    recurseDelete(directory + "/" + dirName);
                }
                foreach (var fileName in isf.GetFileNames(directory + "/*.*"))
                {
                    isf.DeleteFile(directory + "/" + fileName);
                }

                if (!string.IsNullOrEmpty(directory))
                {
                    isf.DeleteDirectory(directory);    
                }
                
            }
        }

        public static bool FileExists(string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return isf.FileExists(fileName);
            }
        }

        public static void Save(string data, string fileName)
        {
            System.Text.UTF8Encoding enc = new UTF8Encoding();
            byte[] result = enc.GetBytes(data);
            Save(result, fileName);
        }

        public static void Save(byte[] data, string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isf.DirectoryExists(Path.GetDirectoryName(fileName)))
                {
                    isf.CreateDirectory(Path.GetDirectoryName(fileName));
                }
                using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(fileName, System.IO.FileMode.Create, isf))
                {
                    fs.Write(data, 0, data.Length);
                    fs.Close();
                }
            }
        }

        public static String LoadString(string fileName)
        {
            byte[] result = Load(fileName);

           

            if(result == null)
            {
                return null;
            }

            UTF8Encoding enc = new UTF8Encoding();
            var output = enc.GetString(result, 0, result.Length);
            return output;

        }

       

        public static byte[] Load(string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(fileName))
                {
                    using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(fileName, System.IO.FileMode.Open, isf))
                    {
                        Debug.WriteLine("Loaded from FS: {0} bytes - {1}", fs.Length, fileName);
                        byte[] result = new byte[fs.Length];
                        fs.Read(result, 0, result.Length);
                        fs.Close();
                        return result;
                    }
                }
                else
                {
                    return null;
                }

            }
        }
    }
}
